#include "Window.h"
#include "ff.h"
#include "stm32f10x.h"
#include "ESP8266.h"
extern ESP8266 Network;
extern USART cout;
extern bool isConfigSucc;
extern char DeskName[];
#define ID_BUTTON_WIFI (GUI_ID_USER+0)
#define ID_BUTTON_EXIT (GUI_ID_USER+1)
#define ID_BUTTON_REMOVE (GUI_ID_USER+2)
#define ID_BUTTON_SAVE (GUI_ID_USER+3)
#define ID_DROPDOWN_DESK (GUI_ID_USER+4)
#define ID_FRAM (GUI_ID_USER+5)
#define ID_FRAM_TEXT (GUI_ID_USER+6)
#define ID_FRAM_1 (GUI_ID_USER+7)
#define ID_FRAM_TEXT_1 (GUI_ID_USER+8)
WM_HWIN hSet;
TEXT_Handle thWIFI;
static TaskHandle_t Config_Task_Handle;
char namebuf[sizeof(Config.DeskName)];
//static void _cbFra(WM_MESSAGE* pMsg)
//{
//	switch(pMsg->MsgId)
//	{
//		case WM_PAINT:
//			break;
//		default:
//			FRAMEWIN_Callback(pMsg);
//			break;
//	}
//}
static void _cbFra(WM_MESSAGE* pMsg)
{
	switch(pMsg->MsgId)
	{
		case WM_PAINT:
			break;
		case WM_DELETE:
			if(not isConfigSucc)
				vTaskDelete(Config_Task_Handle);
			isConfigSucc = false;
			Network.StopSmartConfig();
			break;
		default:
			FRAMEWIN_Callback(pMsg);
	}
}
static void _cbSet(WM_MESSAGE* pMsg)
{
	switch(pMsg->MsgId)
	{
			DROPDOWN_Handle hDropdownDesk;
		case WM_CREATE:
		{
			namebuf[0] = 0;
			hDropdownDesk = DROPDOWN_CreateEx(120, 52, 110, 100, pMsg->hWin, WM_CF_SHOW, DROPDOWN_CF_AUTOSCROLLBAR, ID_DROPDOWN_DESK);
			DROPDOWN_AddString(hDropdownDesk, Config.DeskName);
			res = f_opendir(&dir, "/Pic");
			if(res != FR_OK)
				cout << "diropen fail" << "res:" << res << endl << __FILE__ << ':' << __LINE__ << endl;
			while(true)
			{
				res = f_readdir(&dir, &info);
				if(res or info.fname[0] == 0)
					break;
				DROPDOWN_AddString(hDropdownDesk, info.fname);
			}
			f_closedir(&dir);
			BUTTON_SetText(
			    BUTTON_CreateEx(200, 25, 60, 20, pMsg->hWin,
			                    WM_CF_SHOW, 0, ID_BUTTON_WIFI), "Set WIFI");
			BUTTON_SetText(
			    BUTTON_CreateEx(5, 110, 20, 20, pMsg->hWin,
			                    WM_CF_SHOW, 0, ID_BUTTON_EXIT), "<");
			BUTTON_SetText(
			    BUTTON_CreateEx(65, 195, 90, 20, pMsg->hWin,
			                    WM_CF_SHOW, 0, ID_BUTTON_REMOVE), "Remove SDCard");
			BUTTON_SetText(
			    BUTTON_CreateEx(165, 195, 90, 20, pMsg->hWin,
			                    WM_CF_SHOW, 0, ID_BUTTON_SAVE), "Save Setting");
			break;
		}
		case WM_PAINT:
		{
			GUI_SetBkColor((0xAA << 24) | GUI_WHITE);
			GUI_Clear();
			char buf[20];
			snprintf(buf, sizeof(buf), "WIFI:%s", Config.WIFIName);
			GUI_SetColor(GUI_BLACK);
			GUI_SetFont(GUI_FONT_20_1);
			GUI_DispStringAt(buf, 40, 25);
			GUI_DispStringAt("DeskTop:", 40, 50);
			GUI_DispStringAt("System Infomation", 90, 75);
			GUI_DispStringAt("OS:FreeRTOS", 40, 95);
			snprintf(buf,sizeof(buf),"Memory:%.2f%%",(1.0-(float)xPortGetFreeHeapSize()/(float)configTOTAL_HEAP_SIZE)*100.0);
			GUI_DispStringAt(buf, 40, 115);
			GUI_DispStringAt("DriverLib:V2.0", 40, 135);
			snprintf(buf, sizeof(buf), "Compile Date:%s", __DATE__);
			GUI_DispStringAt(buf, 40, 155);
			break;
		}
		case WM_NOTIFY_PARENT:
		{
			int NCode = pMsg->Data.v;
			int Id = WM_GetId(pMsg->hWinSrc);
			switch(Id)
			{
				case ID_BUTTON_EXIT:
					if(NCode == WM_NOTIFICATION_RELEASED)
					{
						if(namebuf[0] != 0)
							strcpy(Config.DeskName, namebuf);
						WM_DeleteWindow(pMsg->hWin);
						CreateMainWindow();
					}
					break;

				case ID_BUTTON_REMOVE:
					if(NCode == WM_NOTIFICATION_RELEASED)
					{
						//unmount SDcard
						f_unmount("0:");
						GUI_MessageBox("SDCard Unmount Succ", "", GUI_MESSAGEBOX_CF_MOVEABLE);
					}
					break;

				case ID_BUTTON_SAVE:
					if(NCode == WM_NOTIFICATION_RELEASED)
					{
						UINT num;
						//Save Setting
						res = f_open(&fil, "0:/config", FA_WRITE | FA_OPEN_ALWAYS);
						if(res != FR_OK)
							cout << "file open fail" << endl << "res:" << res << endl << __FILE__ << ':' << __LINE__ << endl;
						if(namebuf[0] != 0)//已经更改桌面
						{
							res = f_write(&fil, namebuf, sizeof(namebuf), &num);
							if(res != FR_OK)
								cout << "file Write fail" << endl << "res:" << res << endl << __FILE__ << ':' << __LINE__ << endl;
							res = f_write(&fil, &Config.WIFIName, sizeof(Config.WIFIName), &num);
							if(res != FR_OK)
								cout << "file Write fail" << endl << "res:" << res << endl << __FILE__ << ':' << __LINE__ << endl;
						}
						else//无更改桌面
						{
							res = f_write(&fil, &Config, sizeof(Config), &num);
							if(res != FR_OK)
								cout << "file Write fail" << endl << "res:" << res << endl << __FILE__ << ':' << __LINE__ << endl;
						}
						f_close(&fil);
						FRAMEWIN_Handle hFra = FRAMEWIN_CreateEx(100, 90, 110, 60, pMsg->hWin, WM_CF_SHOW, FRAMEWIN_CF_MOVEABLE | FRAMEWIN_CF_ACTIVE, ID_FRAM, "", nullptr);
						TEXT_CreateEx(0, 0, 100, 38, WM_GetClientWindow(hFra), WM_CF_SHOW, TEXT_CF_HCENTER | TEXT_CF_VCENTER, ID_FRAM_TEXT, "Config Save Succ");
						FRAMEWIN_AddCloseButton(hFra, FRAMEWIN_BUTTON_RIGHT, 0);
					}
					break;

				case ID_DROPDOWN_DESK:
					if(NCode == WM_NOTIFICATION_SEL_CHANGED)
					{
						auto handle = WM_GetDialogItem(pMsg->hWin, ID_DROPDOWN_DESK);
						auto index = DROPDOWN_GetSel(handle);
						res = f_opendir(&dir, "/Pic");
						if(res != FR_OK)
							cout << "diropen fail" << "res:" << res << endl << __FILE__ << ':' << __LINE__ << endl;
						while(index--)
						{
							res = f_readdir(&dir, &info);
							cout << info.fname << endl;
						}
						cout << "Final:" << info.fname << endl;
						strcpy(namebuf, info.fname);
						f_closedir(&dir);
					}
					break;

				case ID_BUTTON_WIFI:
					if(NCode == WM_NOTIFICATION_RELEASED)
					{
						
						//Config WIFI
						FRAMEWIN_Handle hFra = FRAMEWIN_CreateEx(100, 90, 110, 60, pMsg->hWin, WM_CF_SHOW, FRAMEWIN_CF_MOVEABLE | FRAMEWIN_CF_ACTIVE, ID_FRAM_1, "", _cbFra);
						thWIFI = TEXT_CreateEx(0, 0, 100, 38, WM_GetClientWindow(hFra), WM_CF_SHOW, TEXT_CF_HCENTER | TEXT_CF_VCENTER, ID_FRAM_TEXT_1, cn[13]);
						TEXT_SetFont(thWIFI,&UTF_8);
						FRAMEWIN_AddCloseButton(hFra,FRAMEWIN_BUTTON_RIGHT,0);
						Network.SetSmartConfig(ESP8266::ESP_Touch);//使用ESP—Touch协议进行配网
						isConfigSucc = false;
						xTaskCreate(SmartConfig_Task,"Config",256,nullptr,1,&Config_Task_Handle);
					}
					break;
				default:
					break;
			}
		}
		break;
		default:
			WM_DefaultProc(pMsg);
			break;
	}
}
void CreateSettingWindow()
{
	hSet = WM_CreateWindow(0, 0, LCD_GetXSize(), LCD_GetYSize(),
	                       WM_CF_SHOW | WM_CF_HASTRANS, _cbSet, 0);
}
